<template>
  <div>
    <work-template @submitOn="submitOn" :flowId="flowId" @closeSelectedTag="closeSelectedTag" :butShow="bool">
      <div slot="form" class="container-div">
        <purchase-basics-info v-model="detailForm" :state="isEdit" ref="refPurchaseBasicsInfo" />
        <purchase-table v-model="detailForm" ref="refPurchaseTable" :state="isEdit" />
      </div>
    </work-template>

  </div>
</template>

<script>
import {
  getTaxDetails,
  addPrchaseTax,
  prchaseTaxDetail,
} from "@/api/cwgd/prchaseTaxApi.js";


import purchaseBasicsInfo from '../components/purchaseTax/purchaseBasicsInfo.vue'
import purchaseTable from '../components/purchaseTax/purchaseTable.vue'
import fundFeedback from '../components/purchaseTax/fundFeedback.vue'
import uploadPurchase from "../components/purchaseTax/uploadPurchase.vue";

export default {
  dicts: ['work_order_type', 'online_offline'],
  name: "PurchaseTaxDetails",
  components: {
    purchaseBasicsInfo,
    purchaseTable,
    fundFeedback,
    uploadPurchase
  },

  data() {
    return {
      tableData: [],
      showCity: false,
      citySelection: [],
      checkedNodesList: {},

      sumPrice: "",
      driveBookUrl: "",
      isForm: true,
      method: {},
      inspectDetail: {},
      // 表单参数
      detailForm: {
        expectStartTime: '',
        expectEndTime: "",
        carNum: "",
        totalPrice: "",
        dtoList: [],
        workOrderStatus: "",
      },

    };
  },
  watch: {},
  computed: {

    isShow() {

      return true

    },
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId
      }
    }

  },
  created() {
    this.method = this.$route.query
    if (this.$route.query.id) {
      this.handlePrchaseTaxDetail()
    }


    if (this.$route.query.type === 'view') { //  工单id为空
      this.handleGetTaxDetails(this.$route.query.businessDataId)
    }

  },
  mounted() {

  },
  methods: {
    //获取购置税工单
    handlePrchaseTaxDetail() {
      let carIds = this.method.id.split(",")
      prchaseTaxDetail(carIds).then((res) => {
        this.detailForm.carNum = res.length
        this.detailForm.dtoList = res
        this.handeCount()
      })
    },

    handeCount() {
      let num = 0
      for (let i = 0; i < this.detailForm.dtoList.length; i++) {
        num += Number(this.detailForm.dtoList[i].purchaseTaxPrice)
      }
      this.detailForm.totalPrice = num
    },

    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({ visitedViews }) => {
        this.$router.push({
          path: "/cwgd/purchaseTaxList",
        })
      })
    },

    // 提交操作
    submitOn() {
      let arr = [
        this.$refs.refPurchaseBasicsInfo.handleEffect(),
        this.$refs.refPurchaseTable.handleEffect()
      ]
     Promise.all(arr).then(() => {
        let wid = this.method.id
        this.detailForm.idList = wid.split(",")
        this.detailForm.workOrderStatus = '2'
        const loading = this.$loading({
          lock: true,
          text: '保存中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        addPrchaseTax(this.detailForm).then((res) => {
          if (res.code === 200) {
            loading.close();
            this.closeSelectedTag(this.$route)
          }
        }).catch(() => {
          loading.close();
        })

      }).catch(() => {
      })

    },


    //  详情回显
    handleGetTaxDetails(val) {
      getTaxDetails(val).then(res => {
        this.isForm = true
        this.detailForm = res.data
        this.handeCount()
      })
    }
  }
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>
